package com.fiberhome.nas.feature.taskcenter.viewmodel

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.fiberhome.nas.core.model.entity.UploadTask
import com.fiberhome.nas.feature.common.manager.UploadManager
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch
import javax.inject.Inject

/**
 * 任务中心ViewModel
 */
@HiltViewModel
class TaskCenterViewModel @Inject constructor(
    private val uploadManager: UploadManager
) : ViewModel() {

    // 上传任务列表
    private val _uploadTasks = MutableStateFlow<List<UploadTask>>(emptyList())
    val uploadTasks: StateFlow<List<UploadTask>> = _uploadTasks

    // 下载任务列表
    private val _downloadTasks = MutableStateFlow<List<UploadTask>>(emptyList())
    val downloadTasks: StateFlow<List<UploadTask>> = _downloadTasks

    // 当前选中的任务ID集合
    private val _selectedTaskIds = MutableStateFlow<Set<String>>(emptySet())
    val selectedTaskIds: StateFlow<Set<String>> = _selectedTaskIds

    // 是否处于编辑模式
    private val _isEditMode = MutableStateFlow(false)
    val isEditMode: StateFlow<Boolean> = _isEditMode

    // 当前选中的Tab索引
    private val _selectedTabIndex = MutableStateFlow(0)
    val selectedTabIndex: StateFlow<Int> = _selectedTabIndex

    init {
        // 从UploadManager获取上传任务列表
        viewModelScope.launch {
            uploadManager.loadTasksFromDatabase()
            uploadManager.uploadTasks.collect { tasks ->
                _uploadTasks.value = tasks
            }
        }
    }

    /**
     * 暂停上传任务
     */
    fun pauseUploadTask(taskId: String) {
        uploadManager.pauseTask(taskId)
    }

    /**
     * 恢复上传任务
     */
    fun resumeUploadTask(taskId: String) {
        uploadManager.resumeTask(taskId)
    }

    /**
     * 取消上传任务
     */
    fun cancelUploadTask(taskId: String) {
        uploadManager.cancelTask(taskId)
    }

    /**
     * 切换编辑模式
     */
    fun toggleEditMode() {
        _isEditMode.update { !it }
        // 退出编辑模式时清空选中状态
        if (!_isEditMode.value) {
            _selectedTaskIds.value = emptySet()
        }
    }

    /**
     * 设置当前选中的Tab索引
     */
    fun setSelectedTabIndex(index: Int) {
        _selectedTabIndex.value = index
    }

    /**
     * 选择或取消选择任务
     */
    fun toggleTaskSelection(taskId: String) {
        _selectedTaskIds.update { currentSelection ->
            if (currentSelection.contains(taskId)) {
                currentSelection - taskId
            } else {
                currentSelection + taskId
            }
        }
    }

    /**
     * 全选
     */
    fun selectAll() {
        viewModelScope.launch {
            val allTaskIds = if (_selectedTabIndex.value == 0) {
                _uploadTasks.value.map { it.id }.toSet()
            } else {
                _downloadTasks.value.map { it.id }.toSet()
            }
            _selectedTaskIds.value = allTaskIds
        }
    }

    /**
     * 取消全选
     */
    fun deselectAll() {
        _selectedTaskIds.value = emptySet()
    }

    /**
     * 删除选中的任务
     */
    fun deleteSelectedTasks() {
        viewModelScope.launch {
            val selectedIds = _selectedTaskIds.value
            if (selectedIds.isNotEmpty()) {
                if (_selectedTabIndex.value == 0) {
                    // 删除上传任务
                    selectedIds.forEach { taskId ->
                        cancelUploadTask(taskId)
                    }
                } else {
                    // 删除下载任务
                    _downloadTasks.update { tasks ->
                        tasks.filter { it.id !in selectedIds }
                    }
                }
                _selectedTaskIds.value = emptySet()
            }
        }
    }
}